﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartSpace.Core;
using SsTool.Core.Windows;

namespace SsTool.Core
{
    /// <summary>
    /// Interaction logic for OrgPage.xaml
    /// </summary>
    public partial class OrgPage : Page
    {
        public OrgPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(OrgPage_Loaded);
        }

        void OrgPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadDatas();
        }

        private void LoadDatas()
        {
            ICoreService service = App.Get<ICoreService>();
            _ctlTreeOrg.ItemsSource = service.GetOrgs(null, true);
        }

        private void _ctlBtnCreate_Click(object sender, RoutedEventArgs e)
        {
            Org selectedOrg = _ctlTreeOrg.SelectedItem as Org;
            OrgWindow window = new OrgWindow(selectedOrg, true);
            if (window.ShowDialog() == true)
            {
                LoadDatas();
            }
        }

        private void _ctlTreeOrg_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            Org selectedOrg = _ctlTreeOrg.SelectedItem as Org;
            if (selectedOrg != null)
            {
                ICoreService service = App.Get<ICoreService>();
                _ctlGridMember.ItemsSource = service.GetMembers(selectedOrg,0, 999);
            }
        }
    }
}
